package design.create_factory;

import org.junit.Test;

/**
 * 简单工厂
 */
public class DemoSimpleFactory {
    @Test
    public void test() {
        Phone phone=PhoneFactory.makePhone("huawei");
        phone.welcome();
    }

    public static class PhoneFactory {
        private PhoneFactory() {
        }

        public static Phone makePhone(String type) {
            Phone phone = null;
            //其实此处可以不用switch，将类的全限定名作为type传入，然后通过反射创建对象，如Class.forName(type).newInstance()
            switch (type) {
                case "xiaomi":
                    phone = new XiaoMiPhone();
                    break;
                case "huawei":
                    phone = new HuaWeiPhone();
                    break;
                default:
                    new RuntimeException();
            }
            return phone;
        }
    }
}


